import random

import Mis
import YonghuProgram

store = (YonghuProgram.user1, YonghuProgram.user2, YonghuProgram.user3,
         YonghuProgram.user4)


def registration():
    while True:
        a = int(input("请输入用户账号："))
        if len(str(a)) != 8:
            print("格式错误！请重新输入：")
            a = int(input("请输入用户账号："))
        if a in Mis.zhanghao:
            print("用户已存在，请重新输入账号！")
            continue
        elif a not in Mis.zhanghao:
            b = random.randint(10000000, 99999999) and random.choice(Mis.zhanghao)
            if len(store) <= 100:
                print("您的账号是：%08d" % b)
                Mis.zhanghao.append(b)
                YonghuProgram.add()
                print("用户创建成功！")
                return 1
            elif len(store) == 100:
                print("用户库已满！")
                return 3


def saving():
    a = int(input("请输入用户账号："))
    if a not in Mis.zhanghao:
        return False
    elif a in Mis.zhanghao:
        d = int(input("请输入存款金额："))
        c = Mis.zhanghao.index(a)
        Mis.cash[c] = d
        print("存款成功！")


def get():
    a = int(input("请输入用户账号："))
    if a not in Mis.zhanghao:
        print("账户不存在！")
        return 1
    elif a in Mis.zhanghao:
        q = int(input("请输入密码："))
        if len(str(q)) != 6:
            print("密码格式输入错误！")
            q = int(input("请输入密码："))
        c = Mis.zhanghao.index(a)
        if q != Mis.password[c]:
            print("密码错误！")
            return 2
        elif q == Mis.password[c]:
            d = int(input("请输入取款金额："))
            if d > Mis.cash[c]:
                print("余额不足！")
                return 3
            elif d <= Mis.cash[c]:
                Mis.cash[c] -= d
                print("取款成功！")


def transfer():
    iner = int(input("请输入转入账号："))
    outer = int(input("请输入转出账号："))
    if iner or outer not in Mis.zhanghao:
        print("转入账号或转出账号不存在！")
        return 1
    elif iner and outer in Mis.zhanghao:
        iner_password = input("请输入密码：")
        outer_password = input("请输入密码：")
        a = Mis.zhanghao.index(iner)
        b = Mis.zhanghao.index(outer)
        if iner_password != Mis.password(a) or outer_password != Mis.password(b):
            print("转入密码错误或转出密码错误！")
            return 2
        elif iner_password == Mis.password(a) and outer_password == Mis.password(b):
            c = int(input("请输入转帐金额："))
            if c > Mis.cash[b]:
                print("转出账号余额不足！")
                return 3
            elif c <= Mis.cash[b]:
                Mis.cash[b] -= c
                Mis.cash[a] += c
                print("转账成功！账号：%08d，转出%d元，账号：%08d，转入%d元！" % outer, iner)


def search():
    a = int(input("请输入用户账号："))
    if len(str(a)) != 8:
        print("格式错误！请重新输入：")
        a = int(input("请输入用户账号："))
    if a not in Mis.zhanghao:
        print("该用户不存在！")
    elif a in Mis.zhanghao:
        q = input("请输入密码：")
        if len(str(q)) != 6:
            print("密码格式错误！")
            q = int(input("请输入密码："))
        c = Mis.zhanghao.index(a)
        if q != Mis.password[c]:
            print("密码不正确！")
        elif q == Mis.password[c]:
            print("当前账号：%d\n" % Mis.zhanghao[c])
            print("当前账号：%d\n" % Mis.password[c])
            print("当前账号：%d\n" % Mis.cash[c])
            print("当前账号：%d\n" % Mis.address[c])
            print("当前账号：%d\n" % Mis.bank[c])
